	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_AJAREKS.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP,$login['staff_auth']);
	
	

	$docsv_import = FALSE;
	
	$csvf = Class_FILE_CSV::get_upload_file();
	
	if ($csvf) {
		// ロケール情報の変更

		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		
		$rowall = Class_FILE_CSV::fget_rows($csvf);
		
		if  (!empty($rowall)) {
			$col_heads = array(
				'login_id' , //ログインID',  1),
				''         , //ユーザ名',    1),user_name
				''         , //'区分',        1),	auth	
				//'zone_name', //'ゾーン名',        1),	auth	
				'shop_cd'  , //店舗コード',  1),
				''         , //店舗名',      1),		shop_name	
				''         , //更新日時',          3),u_time
				''         , //登録日時',          3),c_time
				''           //更新者',            1)m_user_name
			);
			
			$csvheads  = array();
			$col_index = array();
			foreach ($col_heads as $k => $v) {
				if (!empty($v)) {
					$csvheads[$k]  = $v;
					$col_index[$v] = $k;
				}
			} 
			
			$index_login_id       = $col_index['login_id'];      //
			$col_login_id         = $index_login_id + 1;
			
			$index_shop_cd        = $col_index['shop_cd'];      //
			$col_shop_cd          = $index_shop_cd + 1;
			
			$err = new Class_ERROR("","");
			$dao = new Class_M_AJAREKS;
			
			$ngline      = 0;
			$error_times = 0;
			$error_stop  = IMPORT_STOP_MAX_ERROR_TIMES;
			
			$errtext = array();
			$isng = FALSE;
			$chk = $dao->get_checks();
			$page = NULL;
			
			$shop_arr     = array();
			$shops        = Class_M_OTHER::get_shop( NULL, $page);
			foreach ($shops as $k => $v) {
				$shop_arr[$v["shop_cd"]] = $v["shop_name"];
			}
			
			$bylogin      = TRUE;
			$login_arr    = Class_M_OTHER::get_ajaeks_array(NULL,NULL, $bylogin);
			//print_r($login_arr);
			//整形チェック
			$shop_users = array();
			foreach ($rowall as $line => $row) {
				//ログインＩＤ
				if ( !empty($row[$index_login_id])){
					if (empty($login_arr[$row[$index_login_id]])) {
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][] = "{$line}行{$col_login_id}列目：ユーザーは存在しません。</br>";
					}else {
						$loginv = $login_arr[$row[$index_login_id]];
						$rowall[$line]["auth"] = $loginv["auth"];
						
						if ($loginv["auth"] == "3") {
						//ゾーン入力必須
							
						}
						
						$shop_users[$loginv["auth"]][$row[$index_shop_cd]][] = $row[$index_login_id];
					}
				} 
			}
			
			$linecount = count($rowall);
			$percentold = 100;
 			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				$percent = intval($line/$linecount * 100)."%";
				if  ( $percent != $percentold ) {
					Class_FILE_CSV::write_msg_checking_per( $percent ) ;
				}
				$percentold = $percent;
				$isng = FALSE;
			 	if ( $row[$index_shop_cd] ) {
					foreach ( $csvheads as $k=>$v ) {
						$msg =  $err->check( $rowall[$line][$k], array( "EXIST" )); 
						if (!empty($msg)) {
							$isng = TRUE;
							$col  = $k+1;
							$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
							$error_times++;					
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				}
			
				
				 //一店舗対して、複数ajaチェック
				//店舗コード
				if ( !empty($row[$index_shop_cd])){
					$shop_cd = $row[$index_shop_cd];
					if (empty($shop_arr[$shop_cd])) {
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][] = "{$line}行{$col_shop_cd}列目：店舗は存在しません。</br>";
					} else {
						foreach($shop_users[$row["auth"]][$shop_cd]   as $kk => $vv ) {
							if ($vv  != $row[$index_login_id]) {
								$isng = TRUE;
								$error_times++;
								$ngMsg_tmp[$line][] = "{$line}行店舗コード[$shop_cd]は重複しています。</br>";
								break;
							}
						}
					}
				}
				//CSV内部重複不可チェック
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
					    $rowkey = $row[$index_login_id] ."_".$row[$index_shop_cd];
					    $nowkey = $vv[$index_login_id] ."_".$vv[$index_shop_cd];
						if ($rowkey == $nowkey){
							$col1 = $index_login_id + 1;
							$col2 = $index_shop_cd + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の({$col1}+ {$col2})列目の値重複不可。";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				} 
			
				
				
				//for (i)lnews1
				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
				
				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );	
				}
			} else {
				//DBへ更新
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
					$dao->delAja();
					foreach ( $rowall as $k=>$v ) {
						$i++;
						$percent = intval($i/$line * 100)."%";
						if  ( $percent != $percentold ) {
							Class_FILE_CSV::write_msg_doing( $percent ) ;
						}
						$percentold = $percent;
						$values  = array();
						/*
						foreach ( $csvheads as $kk => $vv ) {
							$values[$vv] = $v[$kk];
						}
						*/
						

						$values["shop_cd"]      =  $v[$index_shop_cd];
						
						$values["user_name"]   =  $login_arr[$v[$index_login_id]]["user_name"];
						$values["userid"]      =  $login_arr[$v[$index_login_id]]["id"];
						/*$keyvalues =  $values[$col_heads[$key1]];
						$where = sprintf(" {$col_heads[$key1]} = '%s'", $keyvalues );
						
						if ( $dao->exits( $where ) ) {
							if (!($dao->editbycol($keyvalues ,$values ,$col_heads[$key1] ))) {
								throw exception("majareks_import","db更新", AOKI_OTHER, "File not opend");
							}
						} else {
						*/
							if ( !( $dao->add($values ) ) ) {
								throw new Exception("majareks_import db更新");
							}
						/*
						}
						*/
					}

					$dao->commit_trans();
					Class_FILE_CSV::write_msg_succed($line, "majareks");
				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}
			} 
		}else {
			$errHtml = "CSVファイルエラー";
			Class_FILE_CSV::write_msg_error( $errHtml );
			
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	